##
## Makefile for Piano Application
##
## Description: This Makefile compiles the piano application with SDL2, SDL2_mixer,
##              and midifile library dependencies.
##

CXX = g++
CXXFLAGS = -std=c++17 -Wall -O2 -I./include -I/opt/homebrew/include
LDFLAGS = -L./lib -lmidifile `sdl2-config --libs` -lSDL2_mixer

# Midifile library
LIBRARY = lib/libmidifile.a



##############################
##
## Targets:
##

# targets which don't actually refer to files
.PHONY : all info library tools programs bin options clean lib


all: info library programs lib ${EXE} 


info:
	@echo ""
	@echo This makefile will compile the Midifile library and/or
	@echo the Midifile programs.  You may have to make the library
	@echo first if compiling the programs. Type one of the following:
	@echo "   make library"
	@echo or
	@echo "   make programs"
	@echo ""
	@echo To compile a specific program called xxx, type:
	@echo "   make xxx"
	@echo ""
	@echo Typing \"make\" alone will compile both the library and all programs.
	@echo ""


lib: library
library:
	$(MAKE) -f Makefile.library


clean:
	$(MAKE) -f Makefile.library clean
	-rm -rf bin
	-rm -rf lib


readme:
	(cd tools; ./.makeReadme)


tools:    programs
bin:      programs
examples: programs
programs:
	$(MAKE) -f Makefile.programs


options:
# The Options class is borrowed from optionlib:
#     https://github.com/craigsapp/optionlib
# This code downloads the most recent version of the Option class, and the
# Option class should not be modified within this library.  The Option
# class is only needed by some of the example programs.  It is not 
# necessary if you only want to use the compiled MidiFile library.
ifneq ($(shell which wget),)
	wget https://raw.githubusercontent.com/craigsapp/optionlib/master/src/Options.cpp -O src/Options.cpp
	wget https://raw.githubusercontent.com/craigsapp/optionlib/master/include/Options.h -O include/Options.h
else ifneq ($(shell which curl),)
	curl https://raw.githubusercontent.com/craigsapp/optionlib/master/src/Options.cpp -o src/Options.cpp
	curl https://raw.githubusercontent.com/craigsapp/optionlib/master/include/Options.h -O include/Options.h
endif



##############################
##
## Piano application targets
##

# Piano application object and executable
OBJ = piano.o
EXE = piano

$(EXE): $(OBJ) $(LIBRARY)
	@-mkdir -p bin
	$(CXX) $(OBJ) $(LIBRARY) $(LDFLAGS) -o $@

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Default target
%:
	@-mkdir -p bin
	@echo compiling file $@
	$(MAKE) -f Makefile.programs $@
